<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class BackendUserIdentity extends CUserIdentity
{
    public $admin;
    public $_id;
    public $username;
	/**
	 * Authenticates a adminUser.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
        $adminUser= AdminUser::model()->find('username = :name', array(':name' => $this->username));
        if(!$adminUser){
            $this->errorCode = self::ERROR_USERNAME_INVALID;

        }elseif($adminUser->password != md5($this->password)){
            //var_dump(md5($this->password));
            // 密码错误
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
            //var_dump($this->errorCode);
        }else{
            // 验证通过
            $this->errorCode = self::ERROR_NONE;
            $this->_id = $adminUser->id;
            $this->setState('username', $adminUser->username);
        }
        return !$this->errorCode; // 真通过，假失败
	}

    public function getId() {
        return $this->_id;
    }
}